Shader "Unlit/Normal"
{
    Properties
    {
        _MainTex ("主纹理", 2D) = "white" {}
        _BumpTex ("法线贴图", 2D) = "white" {}
        _DiffuseColor("漫反射颜色",Color) = (1,1,1,1)
        _SpecularClolor("高光颜色",Color) = (1,1,1,1)
        _SpecularScale("高光强度",Range(0,10)) = 1
        _BumpScale("法线缩放",Range(0,10)) = 1
    }
    SubShader
    {

        Pass
        {
            Tags
            {
                "LightMode"="ForwardBase"
            }
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog
            #include "UnityCG.cginc"
            #include "Lighting.cginc"


            struct v2f
            {
                float4 pos:SV_POSITION;
                float3 normal:NORMAL;
                float3 wPos:TEXCOORD0;
                float4 uv:TEXCOORD1;
                fixed3x3 turnMatrix:TEXCOORD2;
            };

            //----属性相关----
            sampler2D _MainTex;
            sampler2D _BumpTex;
            float _BumpScale;
            float4 _MainTex_ST;
            float4 _BumpTex_ST;
            float4 _DiffuseColor;
            float4 _SpecularClolor;
            float _SpecularScale;
            //---------------

            //没有环境光
            fixed4 GetBlinPhong(in float3 normal, in float3 wPos)
            {
                //漫反射颜色(半兰伯特)
                float3 viewDir = normalize(_WorldSpaceCameraPos - wPos);
                float3 lightDir = normalize(_WorldSpaceLightPos0 - wPos);
                fixed4 diffuseColor = _LightColor0 * _DiffuseColor * (0.5 * dot(normal, lightDir) + 0.5);

                //高光反射颜色
                float3 halfDir = normalize(viewDir + lightDir);
                float v = pow(max(0, dot(normal, halfDir)), _SpecularScale);
                fixed4 specularColor = _LightColor0 * v * _SpecularClolor;
                return specularColor + diffuseColor;
            }

            //用纹理采样颜色作为漫反射颜色
            fixed4 GetBlinPhong2(in float3 normal, in float3 wPos,fixed4 colorValue)
            {
                //漫反射颜色
                float3 viewDir = normalize(_WorldSpaceCameraPos - wPos);
                float3 lightDir = normalize(_WorldSpaceLightPos0 - wPos);
                fixed4 diffuseColor = _LightColor0 * colorValue * (0.5 * dot(normal, lightDir) + 0.5);


                // return diffuseColor;
                
                //高光反射颜色
                float3 halfDir = normalize(viewDir + lightDir);
                // float v = pow(max(0, dot(normal, halfDir)), 10-_SpecularScale);
                float v = pow(max(0, dot(normal, halfDir)), _SpecularScale);
                fixed4 specularColor = _LightColor0 * v * _SpecularClolor;
                return specularColor + diffuseColor;
            }

            //有环境光
            //为什么缝隙颜色变黑
            fixed4 GetBlinPhongWithUNITY_LIGHTMODEL_AMBIENT(in float3 normal, in float3 wPos)
            {
                //漫反射颜色(半兰伯特)
                float3 viewDir = normalize(_WorldSpaceCameraPos - wPos);
                float3 lightDir = normalize(_WorldSpaceLightPos0 - wPos);
                fixed4 diffuseColor = _LightColor0 * _DiffuseColor * (0.5 * dot(normal, lightDir) + 0.5);

                //高光反射颜色
                float3 halfDir = normalize(viewDir + lightDir);
                float v = pow(max(0, dot(normal, halfDir)), _SpecularScale);
                fixed4 specularColor = _LightColor0 * v * _SpecularClolor;
                return specularColor + diffuseColor + UNITY_LIGHTMODEL_AMBIENT;
            }
            
            v2f vert(appdata_full v)
            {
                v2f value;
                value.pos = UnityObjectToClipPos(v.vertex);
                value.wPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                value.normal = UnityObjectToWorldNormal(v.normal);


                value.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                value.uv.zw = v.texcoord.xy * _BumpTex_ST.xy + _BumpTex_ST.zw;

                float3 wTangentDir = UnityObjectToWorldDir(normalize(v.tangent));
                float3 wNormalDir = UnityObjectToWorldDir(normalize(v.normal));
                // float3 binDir = cross(wNormalDir, wTangentDir);
                float3 binDir = cross(wTangentDir,wNormalDir);
                // float3 binDir = cross(wNormalDir, wTangentDir)*v.tangent.w;

                value.turnMatrix = float3x3(
                    wTangentDir.x, binDir.x, wNormalDir.x,
                    wTangentDir.y, binDir.y, wNormalDir.y,
                    wTangentDir.z, binDir.z, wNormalDir.z
                );

                return value;
            }

            fixed4 frag(v2f data) : SV_Target
            {
                float4 texColor = tex2D(_MainTex, data.uv.xy);
                float3 normal = mul(data.turnMatrix, UnpackNormal(tex2D(_BumpTex, data.uv.zw)));
                normal *= _BumpScale;
                // return GetBlinPhong(normal, data.wPos) * texColor;
                return GetBlinPhongWithUNITY_LIGHTMODEL_AMBIENT(normal, data.wPos) * texColor;
                return GetBlinPhong2(normal, data.wPos, texColor);
                return UNITY_LIGHTMODEL_AMBIENT;
            }
            ENDCG
        }
    }
}